package com.px.user.service;

import com.px.user.entity.AdminOps;
import com.px.user.mapper.AdminOpsMapper;
import com.px.user.model.RoleCategory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;

import java.util.Collection;

@Slf4j
@Service
public class AdminOpsService {

    @Autowired
    private AdminOpsMapper adminOpsMapper;

    public void setRole(AdminOps adminOps, Authentication authentication){
        Collection roles = authentication.getAuthorities();
        if (roles.contains(new SimpleGrantedAuthority(RoleCategory.ADMIN.getStatus()))) {
            adminOps.setRole(RoleCategory.ADMIN.getStatus());
        } else if (roles.contains(new SimpleGrantedAuthority(RoleCategory.ROOMADMIN.getStatus()))) {
            adminOps.setRole(RoleCategory.ROOMADMIN.getStatus());
        } else if (roles.contains(new SimpleGrantedAuthority(RoleCategory.TEACHER.getStatus()))) {
            adminOps.setRole(RoleCategory.TEACHER.getStatus());
        } else if (roles.contains(new SimpleGrantedAuthority(RoleCategory.CLIENT.getStatus()))) {
            adminOps.setRole(RoleCategory.CLIENT.getStatus());
        }
        adminOps.setUserId(Long.valueOf(authentication.getName()));
    }

    public void insert(AdminOps adminOps){
        adminOpsMapper.insert(adminOps);
    }
}
